import 'package:flutter/material.dart';
import 'package:flutter_shopping/pages/home/components/home_segment.dart';
import 'package:flutter_shopping/pages/home/components/prodcut_item.dart';
import 'package:flutter_shopping/models/product.dart';
import 'package:flutter_shopping/pages/detail/product_detail.dart';
import 'package:flutter_shopping/utils/constants.dart';

///
/// 首页顶部segment选项栏
///
class HomeBody extends StatelessWidget {
  final String title;

  const HomeBody({
    super.key,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        /// 标题
        Padding(
          padding: const EdgeInsets.only(left: 20, bottom: 10),
          child: Text(
            title,
            style: Theme.of(context).textTheme.titleLarge!.copyWith(fontWeight: FontWeight.bold),
          ),
        ),

        /// 选项卡
        const Categories(),

        /// 商品列表
        Expanded(
          child: GridView.builder(
            padding: const EdgeInsets.symmetric(
              horizontal: kDefaultPadding,
              vertical: kDefaultPadding,
            ),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: kDefaultPadding,
              crossAxisSpacing: kDefaultPadding,
              childAspectRatio: 0.75,
            ),
            itemCount: products.length,
            itemBuilder: (context, index) => ProductItem(
              product: products[index],
              onTap: (value) => Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => ProductDetailPage(product: value),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
